package com.ikas.ai.server.schedule.task;

import cn.hutool.core.bean.BeanUtil;
import com.ikas.ai.framework.FlowEventLoop;
import com.ikas.ai.framework.flow.FlowStatus;
import com.ikas.ai.framework.flow.ScoreFlow;
import com.ikas.ai.model.BinaryTuple;
import com.ikas.ai.server.module.appraise.enums.AssessStatusEnum;
import com.ikas.ai.server.module.assessment.model.po.AssessWfResult;
import com.ikas.ai.server.module.assessment.model.po.AssessWfResultDetail;
import com.ikas.ai.server.module.assessment.model.vo.TaskCallBackServiceVo;
import com.ikas.ai.server.module.assessment.service.AssessWfResultDetailService;
import com.ikas.ai.server.module.assessment.service.AssessWfResultService;
import com.ikas.ai.server.module.flow.model.FlowInstance;
import com.ikas.ai.server.module.flow.service.FlowInstanceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 定时执行有效场景且相同设备下权重相加=1的场景
 *
 * @author hu.bowei
 */
@Component
@Slf4j
public class AssessModelTaskService {

    @Resource
    private FlowInstanceService flowInstanceService;

    @Resource
    private AssessWfResultService assessWfResultService;

    @Resource
    private AssessWfResultDetailService assessWfResultDetailService;

    @Resource
    private FlowEventLoop flowEventLoop;

    public void execModelFlow(AssessWfResult awfResult) {
        BinaryTuple<FlowInstance, ScoreFlow> binaryTuple = flowInstanceService.startFlowTask(awfResult);
        FlowInstance flowInstance = binaryTuple.getData1();
        awfResult.setFlowId(flowInstance.getFlowId());
        awfResult.setFlowInstanceId(flowInstance.getId());
        //设置执行状态
        awfResult.setStatus(AssessStatusEnum.IN_PROCESS.ordinal());
        assessWfResultService.updateById(awfResult);
        flowEventLoop.addFlow(binaryTuple.getData2());
    }


    @Transactional(rollbackFor = Exception.class)
    public void flowExecCallBack(TaskCallBackServiceVo callBackVo) {

        AssessWfResult awfResult = assessWfResultService.getAwfResultByFlowInstanceId(callBackVo.getSceneId(), callBackVo.getFlowInstanceId());
        if (null == awfResult || FlowStatus.SUCCESS.getStatus().equals(awfResult.getStatus())) {
            return;
        }
        //修改评估结果
        updateAwfResult(awfResult, callBackVo.getFlowStatus());
        //修改评估结果明细
        saveBatchResultDetail(callBackVo, awfResult.getId());
    }

    /**
     * 修改评估结果
     *
     * @param awfResult
     * @param flowStatus
     */
    private void updateAwfResult(AssessWfResult awfResult, Integer flowStatus) {
        awfResult.setStatus(flowStatus);
        assessWfResultService.updateById(awfResult);
    }

    private void saveBatchResultDetail(TaskCallBackServiceVo callBackVo, Long awfResultId) {
        List<AssessWfResultDetail> detailList = callBackVo.getNodeList().stream().map(p -> {
            AssessWfResultDetail detail = BeanUtil.copyProperties(p, AssessWfResultDetail.class);
            detail.setRid(awfResultId);
            detail.setFlowId(callBackVo.getFlowId());
            detail.setFlowInstanceId(callBackVo.getFlowInstanceId());
            return detail;
        }).collect(Collectors.toList());
        assessWfResultDetailService.saveBatch(detailList);
    }
}